package com.rx.mgt.controller;

import com.alibaba.fastjson.JSON;
import com.rx.mgt.domain.EmailRuleDetailDomain;
import com.rx.mgt.domain.EmailRuleDomain;
import com.rx.mgt.domain.SysSmtpDomain;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.RuleDetailService;
import com.rx.mgt.service.RuleService;
import com.rx.mgt.service.SysSmtpService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxj on 2017/8/15.
 */
@Controller
public class EmailRuleController {


    @Autowired
    private RuleService ruleService;
    @Autowired
    private RuleDetailService ruleDetailService;

    @Autowired
    private SysSmtpService sysSmtpService;
    @Autowired
    DomainService domainService;
    /**
     * 过滤列表
     */

    @RequestMapping("/rule_list.htmlx")
    public String rule_list(HttpServletRequest request){
        List<EmailRuleDomain> list =  ruleService.queryAll();
        if(list!=null &&list.size()>0){
            for(EmailRuleDomain domain:list){
                String id = domain.getId();
                List<EmailRuleDetailDomain> ruleDetailDomainList = ruleDetailService.queryRuleDetailById(id);
                domain.setRuleDetailDomainList(ruleDetailDomainList);
            }
        }
        request.setAttribute("list",list);
        return "rule_list";
    }

    /**
     * 添加过滤规则初始化
     */
    @RequestMapping("/rule-add.htmlx")
    public String ruleAdd(HttpServletRequest request){
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        request.setAttribute("domainList",list);
        return "rule_add";
    }

    /**
     * 添加规则
     */
    @RequestMapping("/ruleSave.htmlx")
    @ResponseBody
    public Map<String,Object> ruleSave(EmailRuleDomain ruleDomain){
        Map<String,Object> rs = new HashMap<String, Object>();

        ruleService.save(ruleDomain);
        rs.put("code","001");
        return rs;
    }

    /**
     * 修改规则
     */
    @RequestMapping("/udpateSave.htmlx")
    @ResponseBody
    public Map<String,Object> udpateSave(EmailRuleDomain ruleDomain){
        Map<String,Object> rs = new HashMap<String, Object>();

        ruleService.update(ruleDomain);
        rs.put("code","001");
        return rs;
    }

    /**
     * 修改规则页面
     */
    @RequestMapping("/update_rule.htmlx")
    public String updateRule(Model model, @RequestParam("id")String id){
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        EmailRuleDomain domain = ruleService.queryRuleById(id);
        model.addAttribute("rule",domain);
        model.addAttribute("detaiList", JSON.toJSONString(domain.getRuleDetailDomainList()));
        return "rule_update";
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteRule.htmlx")
    @ResponseBody
    public String deleteRule(@RequestParam("id")String id){
        ruleService.deleteRuleById(id);
        return "success";
    }

    /**
     * SMTP 重试时间
     */
    @RequestMapping("/smtp_time.htmlx")
    public String smptTimeAddPage(Model model){
       SysSmtpDomain domain =  sysSmtpService.getSmtpTime();
        model.addAttribute("smtpModel",domain);
        return "smtp_time";
    }


    /**
     * 更新SMTP重试时间
     */
    @RequestMapping("/updateSmtpTime.htmlx")
    @ResponseBody
    public String updateSmtpTime(SysSmtpDomain domain){
        sysSmtpService.updateSmtpTime(domain);
        return "success";
    }

    /**
     * 重试smtp时间
     */
    @RequestMapping("/reSetSmtpTime.htmlx")
    @ResponseBody
    public String reSetSmtpTime(){
        sysSmtpService.reSetSmtpTime();
        return "success";
    }
}
